package com.stek.utils;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.stek.R;

public class FragmentUtil {
	public static void transitFragment(FragmentActivity activity,
			int containerId, Fragment fragment, boolean addToBackStack) {
		FragmentTransaction transaction = activity.getSupportFragmentManager()
				.beginTransaction();
		transaction.setCustomAnimations(android.R.anim.fade_in,
				android.R.anim.fade_out).replace(containerId, fragment);
		if (addToBackStack) {
			transaction.addToBackStack(null);
		}
		transaction.commit();
	}

	public static DialogFragment getDialog(final String title,
			final String message, final int resId,
			final OnFragmentDialogClickListener listener) {
		final DialogFragment dialogFragment = new DialogFragment() {
			public void onCreate(Bundle savedInstanceState) {
				super.onCreate(savedInstanceState);
				setStyle(STYLE_NO_TITLE, 0);
			};

			@Override
			public View onCreateView(LayoutInflater inflater,
					ViewGroup container, Bundle savedInstanceState) {
				View rootView = inflater.inflate(resId, container, false);
				TextView tvTitle = (TextView) rootView
						.findViewById(R.id.tv_title);
				TextView tvMessage = (TextView) rootView
						.findViewById(R.id.tv_message);
				Button btnPositive = (Button) rootView
						.findViewById(R.id.btn_positive);
				Button btnNegative = (Button) rootView
						.findViewById(R.id.btn_negative);
				if (null != tvTitle) {
					tvTitle.setText(title);
				}
				if (null != tvMessage) {
					tvMessage.setText(message);
				}

				if (null != btnPositive) {
					btnPositive.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							if (null != listener) {
								dismiss();
								listener.onClick(getDialog(), v.getId(), -1);
							} else {
								dismiss();

							}

						}
					});

				}

				if (null != btnNegative) {
					btnNegative.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							if (null != listener) {
								dismiss();
								listener.onClick(getDialog(), v.getId(), -1);
							} else {
								dismiss();
							}

						}
					});

				}

				return rootView;
			}
		};
		return dialogFragment;
	}

	public static DialogFragment getDialogWithChoiseMode(final String title,
			final String[] choiseList,
			final OnFragmentDialogClickListener listener) {
		DialogFragment dialogFragment = new DialogFragment() {
			public int choiseIndex = -1;

			public void onCreate(Bundle savedInstanceState) {
				super.onCreate(savedInstanceState);
				setStyle(STYLE_NO_TITLE, 0);
			};

			@Override
			public View onCreateView(LayoutInflater inflater,
					 ViewGroup container,
					 Bundle savedInstanceState) {
				View rootView = inflater.inflate(
						R.layout.view_dialog_choise_mode, container, false);
				TextView tvTitle = (TextView) rootView
						.findViewById(R.id.tv_title);
				Button btnPositive = (Button) rootView
						.findViewById(R.id.btn_positive);
				Button btnNegative = (Button) rootView
						.findViewById(R.id.btn_negative);
				final ListView lvChoise = (ListView) rootView
						.findViewById(R.id.lv_choise);
				lvChoise.setAdapter(new ArrayAdapter<String>(getActivity(),
						android.R.layout.simple_list_item_single_choice,
						choiseList));
				lvChoise.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
				lvChoise.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						choiseIndex = position;

					}
				});
				tvTitle.setText(title);
				if (null != btnPositive) {
					btnPositive.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							if (null != listener) {
								dismiss();
								listener.onClick(getDialog(), v.getId(),
										choiseIndex);
							} else {
								dismiss();

							}

						}
					});

				}

				if (null != btnNegative) {
					btnNegative.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							if (null != listener) {
								dismiss();
								listener.onClick(getDialog(), v.getId(),
										choiseIndex);
							} else {
								dismiss();
							}

						}
					});

				}

				return rootView;
			}

		};
		return dialogFragment;
	}

	public static interface OnFragmentDialogClickListener {
		void onClick(Dialog dialog, int buttonId, int choiseIndex);
	}
}
